package yxy.game.pm2.chat.command;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.ChatChannelType;
import yxy.game.pm2.bean.cluster.ClusterBean;
import yxy.game.pm2.chat.module.Banned;
import yxy.game.pm2.chat.module.BaseChatChannelModule;
import yxy.game.pm2.chat.module.ChatHandle;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ChatMessage;

/**
 * 分享战斗、奖励
 */
@MessageType(proto = Command.Share, classtype = ChatMessage.ShareRequest.class)
public class CMD_Share extends Command<ChatMessage.ShareRequest>{

    @Override
    public void execute(MessageSource messageSource, ChatMessage.ShareRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        ChatMessage.ChatChannel channel = request.getChannel();
        String text = request.getText();

        ChatChannelType channelType = ChatChannelType.forNumber(channel.getNumber());

        ChatMessage.ShareResponse.Builder response = ChatMessage.ShareResponse.newBuilder();

        if (Banned.getInstance().isBanned(playerCode)) {
            send(messageSource, response, Result.ChatBanned);// 禁言中
            return;
        }

        PlayerBean playerBean = PlayerBean.get(playerCode);
        ServerBean serverBean = ServerBean.get(playerBean.getServerId());
        AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
        ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId());
        BaseChatChannelModule chatChannelModule = ChatHandle.getChannel(
                clusterBean.getClusterId(),
                areaBean.getGroupId(),
                areaBean.getAreaId(),
                playerBean.getCode(),
                channelType
        );
        if (chatChannelModule == null) {
            send(messageSource, response, Result.ParamError);// 频道关闭中
            return;
        }
        // 判断发送间隔
        boolean intervalTooShort = chatChannelModule.checkIntervalOfBattle(playerCode);
        if (intervalTooShort) {
            send(messageSource, response, Result.ChatSendFrequent);// 发送太频繁
            return;
        }

        if (channelType.equals(ChatChannelType.CHAT_WORLD)) {
            int levelLimit = fomula.跨服聊天等级限制.intValue();
            int viplevelLimit = fomula.跨服聊天VIP等级限制.intValue();
            if (playerBean.getLevel() < levelLimit && playerBean.getViplevel() < viplevelLimit) {
                send(messageSource, response, Result.ChatWorldChannelLevelLow);// 玩家等级不够
                return;
            }
        }
        ChatHandle.send(playerCode, channelType, text);
    }

    private void send(MessageSource messageSource, ChatMessage.ShareResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
